
/*!
 * \file      crazycoloredsquare.cpp
 * \brief     CrazyColoredSquare implementation.
 * \author    Duriez Franck
 * \author    Carton Paul-Henri
 * \version   0.1
 */

#include "assert_macros.h"
#include "constant.h"
#include "crazycoloredsquare.h"

CrazyColoredSquare::CrazyColoredSquare(QSFMLWidget *displayWidget, float *dt) : /*{{{*/
	    Square(displayWidget, dt),
		m_R(sf::Randomizer::Random(SQUARE_MIN_COLOR, 255)),
		m_G(sf::Randomizer::Random(SQUARE_MIN_COLOR, 255)),
		m_B(sf::Randomizer::Random(SQUARE_MIN_COLOR, 255)),
		m_A(sf::Randomizer::Random(SQUARE_MIN_COLOR, 255)),
	    m_shape(sf::Shape::Rectangle(0, 0,
					SQUARE_SIZE, SQUARE_SIZE,
					sf::Color::Color(m_R, m_G, m_B, m_A))),
	    m_time_elapsed_on_destruction(0.f)
{
} /*}}}*/

CrazyColoredSquare::CrazyColoredSquare(QSFMLWidget *displayWidget, float *dt, sf::Color color) : /*{{{*/
	    Square(displayWidget, dt),
	    m_shape(sf::Shape::Rectangle(0, 0, SQUARE_SIZE, SQUARE_SIZE, color)),
	    m_time_elapsed_on_destruction(0.f)
{
} /*}}}*/

CrazyColoredSquare::~CrazyColoredSquare() /*{{{*/
{
} /*}}}*/

void CrazyColoredSquare::moveColorComponant(int& colorComponant) /*{{{*/
{
	// Here the colorComponant is randomly translated and this
	// for each call of this function. So when this kind of
	// Square is display in the game it seems that the Colors
	// are crazy. Moreover, more the dt is near to zero at each call
	// more the comportement of the colorComposant is near to a brownian
	// move in the segment [SQUARE_MIN_COLOR, 255]
	colorComponant += sf::Randomizer::Random(-SQUARE_MAX_CHANGE, SQUARE_MAX_CHANGE);
	if (colorComponant > 255) colorComponant = 255;
	if (colorComponant < SQUARE_MIN_COLOR) colorComponant = SQUARE_MIN_COLOR;
} /*}}}*/

void CrazyColoredSquare::moveColor() /*{{{*/
{
	// Just apply moveColorComponant to each colorComponant
	moveColorComponant(m_R);
	moveColorComponant(m_G);
	moveColorComponant(m_B);
	moveColorComponant(m_A);
} /*}}}*/

/************
 * DRAWABLE *
 ************/
/*{{{*/
void CrazyColoredSquare::draw() const /*{{{*/
{
	displayer->Draw(m_shape);
} /*}}}*/

void CrazyColoredSquare::update() /*{{{*/
{
	// standard square update
	Square::update();

	// specific update
	moveColor();
	m_shape.SetColor(sf::Color::Color(m_R, m_G, m_B, m_A));
} /*}}}*/

// absolute setters
void CrazyColoredSquare::setCenter(Vector2f center) /*{{{*/
{
	m_shape.SetCenter(center);
} /*}}}*/

void CrazyColoredSquare::setPosition(Vector2f position) /*{{{*/
{
	m_shape.SetPosition(position);
} /*}}}*/

void CrazyColoredSquare::setRotation(float angle) /*{{{*/
{
	m_shape.SetRotation(angle);
//    DEBUGGER("m_shape rotation : " << m_shape.GetRotation());
//    DEBUGGER("square rotation  : " << angle);
} /*}}}*/

// relative setters
void CrazyColoredSquare::moveCenter(Vector2f dcenter) /*{{{*/
{
	Vector2f tmp = m_shape.GetCenter() + dcenter;
	m_shape.SetCenter(tmp);
} /*}}}*/

void CrazyColoredSquare::move(Vector2f dposition) /*{{{*/
{
	m_shape.Move(dposition);
} /*}}}*/

void CrazyColoredSquare::rotate(float dangle) /*{{{*/
{
	m_shape.Rotate(dangle);
} /*}}}*/

// getters
Vector2f CrazyColoredSquare::getCenter() const /*{{{*/
{
	return m_shape.GetCenter();
} /*}}}*/

Vector2f CrazyColoredSquare::getPosition() const /*{{{*/
{
	return m_shape.GetPosition();
} /*}}}*/

float CrazyColoredSquare::getRotation() const /*{{{*/
{
	return m_shape.GetRotation();
} /*}}}*/

// description
std::string CrazyColoredSquare::toString() const /*{{{*/
{
	std::ostringstream res;
	res << "position              : (" << getPosition().x << ","
	        << getPosition().y << ")" << std::endl;
	res << "center                : (" << getCenter().x << ","
	        << getCenter().y << ")" << std::endl;
	res << "angle                 : " << getRotation() << std::endl;
	res << "position in container : (" << getPositionInContainer().x << ","
	        << getPositionInContainer().y << ")" << std::endl;
	res << "color                 : (" << (int) m_shape.GetColor().r << ","
	        << (int) m_shape.GetColor().g << ","
	        << (int) m_shape.GetColor().b << ")" << std::endl;
	return res.str();
} /*}}}*/
/*}}}*/

/***********
 * ACTIONS *
 ***********/
/*{{{*/
void CrazyColoredSquare::destroyStartingProcess() /*{{{*/
{

} /*}}}*/

void CrazyColoredSquare::destroyProcess() /*{{{*/
{
	if (m_time_elapsed_on_destruction <= SQUARE_DESTRUCTION_DELAY)
	{
	    destroy.maintain();
	    rotate(-(*dt) * SQUARE_ROTATION_SPEED);
	    m_time_elapsed_on_destruction += *dt;
	    m_shape.SetScale(Vector2f(1.f,1.f)/(1 + m_time_elapsed_on_destruction));
	}
} /*}}}*/

void CrazyColoredSquare::destroyStoppingProcess() /*{{{*/
{
} /*}}}*/
/*}}}*/

